"""
完成企业微信接口特有的鉴权逻辑， 获取access_token
"""
import requests

from frame.apis.contacts.base_api import BaseApi
from frame.utils.log_util import logger
from frame.utils.utils import Utils


class Wework(BaseApi):

    def __init__(self):
        # 获取配置数据
        self.get_config()
        # 定义 base_url
        # self.base_url = "https://qyapi.weixin.qq.com"
        # 类实例化时自动调用方法完成 access_token 获取
        self.token = self.get_access_token()

    def get_access_token(self):
        """
        获取 access_token
        :return:
        """
        # 获取 access_token
        # 定义凭证
        # corpid = "wwf6528473a3d5aba7"
        # corpsecret = "CAXY3jWmbcNRvtXnmlV_rOZvTlVidpifeZ1aFa4dwm8"
        url = f"{self.base_url}/cgi-bin/gettoken"
        # 定义请求参数
        params = {
            "corpid": self.corpid,
            "corpsecret": self.corpsecret
        }
        # r = requests.get(url, params=params)
        req = {
            "method": "GET",
            "url": url,
            "params": params
        }
        r = self.send_api(req)
        print(r.text)
        token = r.json().get("access_token")
        return token

    def get_config(self):
        """
        读取配置文件数据
        :return:
        """
        file_path = "../config/test_env.yaml"
        yaml_data = Utils.get_yaml_data(file_path)
        logger.info(f"配置文件为 {yaml_data}")
        # 获取需要的数据
        self.base_url = yaml_data.get("base_url")
        self.corpid = yaml_data.get("corpid").get("创世神")
        self.corpsecret = yaml_data.get("corpsecret").get("contacts")

